package game.module.qzmj.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.entity.PlayingRole;
import game.module.hall.logic.QipaiConstants;
import game.module.hall.logic.QipaiDataManager;
import game.module.qzmj.bean.QzmjRoom;
import game.session.SessionManager;
import lion.common.ArrayUtility;
import lion.netty4.message.RequestMessage;
import lua.LuaMessageQzmj.PushQzmjGameStart;

public class QzmjLogic {

	private static Logger logger = LoggerFactory.getLogger(QzmjLogic.class);

	static class SingletonHolder {
		static QzmjLogic instance = new QzmjLogic();
	}

	public static QzmjLogic getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 6位数的房间唯一id
	 * 
	 * @return
	 */
	public String generateRoomUuid() {
		return RandomStringUtils.randomNumeric(6);
	}

	public QzmjRoom createMjRoom(int playerId, short roundCount, byte sanjindao, byte eat_card, byte hua_card,
			byte prevent_cheat, int gold_coin, byte cost_type, byte youjin) {
		String roomUuid = QipaiDataManager.getInstance().generateRoomUuid();
		QzmjRoom mjRoom = new QzmjRoom();
		byte ready[] = new byte[4];
		mjRoom.setReady(ready);
		mjRoom.setUuid(roomUuid);
		mjRoom.setRoundCount(roundCount);
		mjRoom.setSanjindao(sanjindao);
		mjRoom.setEat_card(eat_card);
		mjRoom.setHua_card(hua_card);
		mjRoom.setPrevent_cheat(prevent_cheat);
		mjRoom.setGold_coin(gold_coin);
		mjRoom.setCost_type(cost_type);
		mjRoom.setYoujin(youjin);
		List<Integer> playerList = new ArrayList<>();
		playerList.add(playerId);
		mjRoom.setPlayerList(playerList);

		QzmjDataManager.getInstance().addRoom(mjRoom);
		QipaiDataManager.getInstance().addRoomUuid(roomUuid, QipaiConstants.ROOM_TYPE_MJ_QZ);
		return mjRoom;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			String randStr = RandomStringUtils.randomNumeric(6);
			logger.info(randStr);
		}
	}

	public void addRoomPlayer(int playerId, String room_id) {
		QzmjRoom mjRoom = QzmjDataManager.getInstance().getMjRoom(room_id);
		if (mjRoom == null) {
			return;
		}
		mjRoom.getPlayerList().add(playerId);
	}

	public void lanQzGenerateCardsBack(RequestMessage msg) {
		logger.info("lan qzmj generate cards callback");
		PushQzmjGameStart pushGameStart = new PushQzmjGameStart();
		pushGameStart.room_server_host = HallServer.getInstance().getServerConfig().getRsSocketHost();
		pushGameStart.room_server_port = HallServer.getInstance().getServerConfig().getRsSocketPort();
		String roomUuid = msg.readString();
		int playerCount = msg.readInt();
		List<Integer> playerList = new ArrayList<>();
		Map<Integer, List<Byte>> cardsMap = new HashMap<>();
		for (int i = 0; i < playerCount; i++) {
			int playerId = msg.readInt();
			playerList.add(playerId);
			int cardCount = msg.readInt();
			List<Byte> cards = new ArrayList<>();
			for (int j = 0; j < cardCount; j++) {
				byte aCard = msg.readByte();
				cards.add(aCard);
			}

			cardsMap.put(playerId, cards);
		}
		short[] Shaizi_num = new short[2];
		Shaizi_num[0] = msg.readShort();
		Shaizi_num[1] = msg.readShort();
		pushGameStart.Shaizi_num = Shaizi_num;

		for (Map.Entry<Integer, List<Byte>> aEntry : cardsMap.entrySet()) {
			int playerId = aEntry.getKey();
			List<Byte> cardsList = aEntry.getValue();
			PlayingRole pr = SessionManager.getInstance().getPlayer(playerId);
			if (pr != null && pr.isChannelActive()) {
				// pushGameStart.my_cards = ArrayUtility.toByteArray(cardsList);
				pushGameStart.cards = ArrayUtility.toByteArray(cardsList);
				pr.writeAndFlush(pushGameStart.build(pr.alloc()));
			}
		}

		// 清理缓存信息
		QzmjDataManager.getInstance().removeRoom(roomUuid);
		QipaiDataManager.getInstance().removeRoomUuid(roomUuid);
	}
}
